window.addEventListener('load', function () {
    let focus = this.document.querySelector('.focus')
    let ul = focus.children[0]
    let ol = focus.children[1]
    let index = 0
    let w = focus.offsetWidth
    // 移动函数
    function mover(desk) {
        let trans = -index * w
        ul.style.transition = desk
        ul.style.transform = 'translateX(' + trans + 'px)'
    }
    let timer = this.setInterval(function () {
        index++
        mover('all .3s')
    }, 2000)
    ul.addEventListener('transitionend', function () {
        if (index >= 3) {
            index = 0
            mover('none')
        } else if (index < 0) {
            index = 2
            mover('none')
        }
        // 去掉小圆点的类名
        ol.querySelector('.cun').classList.remove('cun');
        // 给小圆点添加新的类名
        ol.children[index].classList.add('cun')
    })
    // 手指滑动图片
    var startx = 0
    var movex = 0
    var flag = false
    ul.addEventListener('touchstart', function (e) {
        startx = e.targetTouches[0].pageX
        clearInterval(timer)
    })
    ul.addEventListener('touchmove', function (e) {
        flag = true
        movex = e.targetTouches[0].pageX - startx
        let trans = -index * w + movex
        ul.style.transition = 'none'
        ul.style.transform = 'translateX(' + trans + 'px)'
        e.preventDefault()
    })
    ul.addEventListener('touchend', function (e) {
        if (flag) {
            if (Math.abs(movex) > 50) {
                if (movex > 0) {
                    index--
                }
                if (movex < 0) {
                    index++
                }
                mover('all .1s')
            } else {
                mover('all .1s')
            }
        }
        clearInterval(timer)
        timer = setInterval(function () {
            index++
            mover('all .3s')
        }, 2000)
    })

    // 返回顶部
    var goback = this.document.querySelector('.goback')
    var nav = this.document.querySelector('nav')
    this.addEventListener('scroll', function () {
        if (window.pageYOffset > nav.offsetTop) {
            goback.style.display = 'block'
        } else {
            goback.style.display = 'none'
        }
    })
    goback.addEventListener('click', function () {
        window.scroll(0, 0)
    })
})